package com.cjxt.ams.controller;


import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cjxt.ams.domain.User;
import com.cjxt.ams.service.UserLoginService;
import com.cjxt.ams.service.UserService;
import com.cjxt.ams.utils.MD5Utils;
import com.cjxt.ams.utils.R;
import com.cjxt.ams.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
/**
 * @author JingYang
 * @version V1.0
 * @Description:
 * @className: UserConroller1
 * @date 2021/11/26 14:17
 * @company:华勤技术股份有限公司
 * @copyright: Copyright (c) 2021
 */
@Controller
@RequestMapping("/admin")
public class UserController extends AbstractController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserLoginService loginService;


    /**
     * @Description: 增加
     * @Param: user
     * @return:
     * @Author: X00004958
     * @date: 2021/12/29
     */
    @PostMapping("/save")
    @ResponseBody
    @Transactional
    public Result<User> save(@RequestBody User user) {
        if (userService.judgeUserAccount(user.getUserAccount())) {
            return Result.error("账号已存在,不允许重复");
        }else {
                user.setCreateBy(getUser().getUsername());
                MD5Utils utils = new MD5Utils();
                String str = "123456";
                user.setPassword(utils.string2MD5(str));
                user.setStatus("Y");
                user.setLockFlag(1);
                userService.save(user);
                return Result.ok("保存成功！");
            }
    }

    /**
     * @Description: 修改
     * @Param: user
     * @return:
     * @Author: X00004958
     * @date: 2021/12/29
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result<User> update(@RequestBody User user) {
        if (getUser().getUserId().equals(user.getUserId())) {
            return Result.error("登录用户信息，不允许修改！");
        } else {
                user.setUpdateBy(getUser().getUsername());
                userService.updateById(user);
                return Result.ok("修改成功！");
            }
        }


    /**
     * @Description: 重置密码
     * @Param: user
     * @return:
     * @Author: X00004958
     * @date: 2021/1/17
     */
    @RequestMapping("/resetPassword")
    @ResponseBody
    public Result<User> resetPassword(@RequestBody User user){

        String pass = "123456";
        MD5Utils utils = new MD5Utils();
        String password = utils.string2MD5(pass);//进行MD5加密
        user.setPassword(password);
        userService.updateById(user);
        return Result.ok("密码修改成功！");
    }

    /**
     * @Description: 删除
     * @Param: userId
     * @return:
     * @Author: X00004958
     * @date: 2021/12/29
     */
    @RequestMapping("/del")
    @ResponseBody
    public Result delete(Integer userId,User user) {
        if (getUser().getUserId().equals(user.getUserId())) {
            return null;
        } else {
                userService.del(userId);
                return Result.ok("删除成功！");
            }
        }

    /**
     * @Description: 展示列表
     * @Param: user
     * @return: Map<String,Object>
     * @Author: X00004958
     * @date: 2021/12/29
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> List(User user) {
        IPage<User> users = userService.selectUser(user);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",users.getTotal());
        map.put("data",users.getRecords());
        return map;
    }

    /**
     * @param useraccount 用户账号
     * @param password    密码
     * @param birth       出生日期
     * @param sex         性别
     * @param email       电子邮箱
     * @param mobile      电话号码
     * @param address     家庭住址
     * @return Result
     * @description: 新用户注册
     * @author XueYunFei
     * @date 2021/12/20 14:39
     */
    @PostMapping("/register")
    @ResponseBody
    public Result register(String useraccount, String password, String mobile, String email, String address, String birth, String sex) {

        User user = new User();

        //用户名称默认为UUID
        String username = userService.getSUUID();
        user.setUsername(username);
        user.setStatus("Y");

        //判断账号是否包含字符
        boolean ba = loginService.judgeContainsStr(useraccount);

        if (useraccount.length() <= 0) {
            return Result.error("账号不能为空");
        }
        if (!ba) {
            return Result.error("账号必须包含字符");
        }
        if (useraccount.length() < 6 || useraccount.length() > 12) {
            return Result.error("账号长度不能小于6位,大于12位");
        }
        if (userService.judgeUserAccount(useraccount)) {
            return Result.error("账号已存在,不允许重复");
        }

        user.setUserAccount(useraccount);

        //判断密码是否包含字符
        boolean bp = loginService.judgeContainsStr(password);
        if (password.length() <= 0) {
            return Result.error("密码不能为空");
        }
        if (!bp) {
            return Result.error("密码必须包含字符");
        }

        MD5Utils utils = new MD5Utils();
        String pass = utils.string2MD5(password);//进行MD5加密
        user.setPassword(pass);


        //验证给定字符串是否是否电话号码格式
        boolean bm = Validator.isMobile(mobile);
        if (mobile.length() <= 0) {
            return Result.error("电话号码不能为空");
        }
        if (!bm) {
            return Result.error("请输入正确的电话号码");
        }
        user.setMobile(mobile);

        //验证给定字符串是否符合电子邮件格式
        boolean be = Validator.isEmail(email);
        if (email.length() <= 0) {
            return Result.error("邮箱不能为空");
        }
        if (!be) {
            return Result.error("请输入正确的邮箱地址");
        }
        user.setEmail(email);

        if (address.length() <= 0) {
            return Result.error("地址不能为空");
        }
        user.setAddress(address);

        //缺省判断日期时间大小
        if (birth.length() <= 0) {
            return Result.error("出生日期不能为空");
        }
        user.setBirth(birth);

        user.setSex(sex);
        user.setCreateBy(username);
        //注册时设置默认普通用户角色

        userService.save(user);
        //同时将用户与角色关联关系保存到用户角色表中

        return Result.ok("注册成功");
    }
}
